using System;
using System.Text;
namespace ServiceStack
{
	public class UrnId
	{
		private const char FieldSeperator = ':';
		private const char FieldPartsSeperator = '/';
		private const int HasNoIdFieldName = 3;
		private const int HasIdFieldName = 4;
		public string TypeName
		{
			get;
			private set;
		}
		public string IdFieldValue
		{
			get;
			private set;
		}
		public string IdFieldName
		{
			get;
			private set;
		}
		private UrnId()
		{
		}
		public static UrnId Parse(string urnId)
		{
			string[] array = urnId.Split(new char[]
			{
				':'
			});
			if (array.Length == 3)
			{
				return new UrnId
				{
					TypeName = array[1],
					IdFieldValue = array[2]
				};
			}
			if (array.Length == 4)
			{
				return new UrnId
				{
					TypeName = array[1],
					IdFieldName = array[2],
					IdFieldValue = array[3]
				};
			}
			throw new ArgumentException("Cannot parse invalid urn: '{0}'", urnId);
		}
		public static string Create(string objectTypeName, string idFieldValue)
		{
			if (objectTypeName.Contains(':'.ToString()))
			{
				throw new ArgumentException("objectTypeName cannot have the illegal characters: ':'", "objectTypeName");
			}
			if (idFieldValue.Contains(':'.ToString()))
			{
				throw new ArgumentException("idFieldValue cannot have the illegal characters: ':'", "idFieldValue");
			}
			return string.Format("urn:{0}:{1}", objectTypeName, idFieldValue);
		}
		public static string CreateWithParts(string objectTypeName, params string[] keyParts)
		{
			if (objectTypeName.Contains(':'.ToString()))
			{
				throw new ArgumentException("objectTypeName cannot have the illegal characters: ':'", "objectTypeName");
			}
			StringBuilder stringBuilder = new StringBuilder();
			for (int i = 0; i < keyParts.Length; i++)
			{
				string value = keyParts[i];
				if (stringBuilder.Length > 0)
				{
					stringBuilder.Append('/');
				}
				stringBuilder.Append(value);
			}
			return string.Format("urn:{0}:{1}", objectTypeName, stringBuilder);
		}
		public static string CreateWithParts<T>(params string[] keyParts)
		{
			return UrnId.CreateWithParts(typeof(T).Name, keyParts);
		}
		public static string Create<T>(string idFieldValue)
		{
			return UrnId.Create(typeof(T), idFieldValue);
		}
		public static string Create<T>(object idFieldValue)
		{
			return UrnId.Create(typeof(T), idFieldValue.ToString());
		}
		public static string Create(Type objectType, string idFieldValue)
		{
			if (idFieldValue.Contains(':'.ToString()))
			{
				throw new ArgumentException("idFieldValue cannot have the illegal characters: ':'", "idFieldValue");
			}
			return string.Format("urn:{0}:{1}", objectType.Name, idFieldValue);
		}
		public static string Create<T>(string idFieldName, string idFieldValue)
		{
			return UrnId.Create(typeof(T), idFieldName, idFieldValue);
		}
		public static string Create(Type objectType, string idFieldName, string idFieldValue)
		{
			if (idFieldValue.Contains(':'.ToString()))
			{
				throw new ArgumentException("idFieldValue cannot have the illegal characters: ':'", "idFieldValue");
			}
			if (idFieldName.Contains(':'.ToString()))
			{
				throw new ArgumentException("idFieldName cannot have the illegal characters: ':'", "idFieldName");
			}
			return string.Format("urn:{0}:{1}:{2}", objectType.Name, idFieldName, idFieldValue);
		}
		public static string GetStringId(string urn)
		{
			return UrnId.Parse(urn).IdFieldValue;
		}
		public static Guid GetGuidId(string urn)
		{
			return new Guid(UrnId.Parse(urn).IdFieldValue);
		}
		public static long GetLongId(string urn)
		{
			return long.Parse(UrnId.Parse(urn).IdFieldValue);
		}
	}
}
